package com.example.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.interfacePackage.RvClickInterface;
import com.example.studyapplication.R;

import java.util.BitSet;
import java.util.List;

/**
 * @Description:
 * @Author: 葛浩杰
 * @Version: 1.0 2021/1/28
 */
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.MyViewHolder> {

    private Context mContext;
    private List<String> mList;
    private RvClickInterface rvClickInterface;
    public static int creatCount = 0;

    public RvAdapter(Context mContext, List<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setOnItemClickListener(RvClickInterface rvClickInterface){
        this.rvClickInterface = rvClickInterface;
    }

    @NonNull
    @Override
    public RvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view  = LayoutInflater.from(mContext).inflate(R.layout.item_main,parent,false);
        Log.e("ghj","onCreateViewHolder");
        return new RvAdapter.MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull RvAdapter.MyViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.btnFunction.setText(mList.get(position));
        Log.e("ghj","onBindViewHolder---" + position);
        holder.btnFunction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rvClickInterface.OnItemClickListener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{

        private Button btnFunction;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            Log.e("ghj","MyViewHolder");
            btnFunction = itemView.findViewById(R.id.btn_function);
            setIsRecyclable(false);
        }
    }

}
